package com.ist412.pocke;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ist412.pocke.DTO.WorkoutDTO;

/**
 * This activity provides CRUD functionality for Workouts. Both scheduled and unscheduled workouts can
 * be created in this menu. In addition, a user can select a Workout to edit or delete.
 * <p>
 * Add	WorkoutAddEditActivity
 * Edit	WorkoutAddEditActivity.java
 * 
 * @author Rob C
 * @version 1.2
 */
public class WorkoutActivity extends Activity implements OnClickListener {
	
	private LinearLayout 		_llyScheduled, _llyUnscheduled;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Remove title & Make fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
        WindowManager.LayoutParams.FLAG_FULLSCREEN);
        
		setContentView(R.layout.activity_workout);
		
		MainActivity.loadData();
		
		_llyScheduled			= (LinearLayout) findViewById(R.id.workout_llyScheduled);
		_llyUnscheduled			= (LinearLayout) findViewById(R.id.workout_llyUnscheduled);
		
		// TODO Get workouts from SQLite
		// Get workouts from main activity
		
		// Empty / null _workouts is handled inside of showWorkouts()
		showWorkouts();
		
		Button btnAddWorkout = (Button) findViewById(R.id.workout_btnAddWorkout);
		btnAddWorkout.setOnClickListener(new WorkoutHandler(false, -1));
		
		
        // Setup navigation
        ImageView imgProfile 	= (ImageView) findViewById(R.id.navigation_imgProfile);
        ImageView imgCalendar 	= (ImageView) findViewById(R.id.navigation_imgCalendar);
        ImageView imgWorkout 	= (ImageView) findViewById(R.id.navigation_imgWorkout);
        ImageView imgExercises	= (ImageView) findViewById(R.id.navigation_imgExercise);
        ImageView imgBack		= (ImageView) findViewById(R.id.workout_imgBack);
        ImageView imgSettings	= (ImageView) findViewById(R.id.workout_imgSettings);
        
        imgProfile.setOnClickListener(this);
        imgCalendar.setOnClickListener(this);
        imgWorkout.setEnabled(false);
        imgWorkout.setImageResource(R.drawable.workouts_icon_trans_dark);
        // TODO Set this through R.color
        imgWorkout.setBackgroundColor(MainActivity._BG_BLUE);
        imgExercises.setOnClickListener(this);
        imgBack.setOnClickListener(this);
        imgSettings.setOnClickListener(this);
		
	} // end onCreate()
	
	@Override
	public void onResume() {
		super.onResume();
		
		// Refresh the menus
		MainActivity.loadData();
		clearWorkoutButtons();
		showWorkouts();
	}
	
	@Override
	public void onClick(View v) {
		Intent intent;
		switch (v.getId()) {
			// Navigation
			case R.id.navigation_imgProfile:
				MainActivity.saveData();
				intent = new Intent(this, ProfileActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.navigation_imgCalendar:
				MainActivity.saveData();
				finish();
				break;
			case R.id.navigation_imgExercise:
				MainActivity.saveData();
				intent = new Intent(this, ExerciseActivity.class);
				startActivity(intent);
				finish();
				break;
			case R.id.workout_imgBack:
				MainActivity.saveData();
				finish();
				break;
			case R.id.workout_imgSettings:
				intent = new Intent(this, SettingsActivity.class);
				startActivity(intent);
				break;
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Fetches WorkoutDTO object(s) from the SQLite database
	 * @return	ArrayList<WorkoutDTO>
	 * 
	 * TODO Implement SQLite to finish this function
	 */
	private final ArrayList<WorkoutDTO> getWorkouts() {
		ArrayList<WorkoutDTO> workouts;
		workouts = new ArrayList<WorkoutDTO>();
		return workouts;
	}
	
	/**
	 * Display a list of all Workouts separated by Scheduled or Unscheduled. This is done by creating
	 * a RelativeLayout to hold the information about the txtName, btnEdit, and btnDelete for each Workout.
	 * 
	 * TODO Use local _workouts once SQLite available
	 */
	private final void showWorkouts() {
		if (MainActivity._workouts.size() <= 0) {
			TextView tv1 = new TextView(this);
			TextView tv2 = new TextView(this);
			tv1.setText("No Scheduled Workouts");
			tv2.setText("No Unscheduled Workouts");
			_llyScheduled.addView(tv1);
			_llyUnscheduled.addView(tv2);
			// Don't allow the remainder of the function to finish
			return;
		}
		
		/*// Sort workouts by schedule or unscheduled
		ArrayList<Workout> scheduled 	= new ArrayList<Workout>();
		ArrayList<Workout> unscheduled 	= new ArrayList<Workout>();
		
		for (Workout w : MainActivity._workouts) {
			if (w.isScheduled()) {
				scheduled.add(w);
			} else {
				unscheduled.add(w);
			}
		}*/
		
		// Display Workouts in relevant sections
		RelativeLayout.LayoutParams rlp;
		RelativeLayout rly;
		TextView tv;
		ImageView imgEdit, imgDelete, imgStart;
		Workout w;
		
		for (int i = 0; i < MainActivity._workouts.size(); i++) {
			w = MainActivity._workouts.get(i);
			
			rly = new RelativeLayout(this);
			rly.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
			rly.setPadding(10, 0, 0, 10);
			
			// Start workout Button
			imgStart = new ImageView(this);
			imgStart.setImageResource(R.drawable.arrow_right);
			imgStart.setId(1001);
			imgStart.setOnClickListener(new WorkoutStarter(i));
			rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
			rlp.addRule(RelativeLayout.CENTER_VERTICAL);
			
			rly.addView(imgStart, rlp);
			
			// Delete Button
			imgDelete = new ImageView(this);
			imgDelete.setImageResource(R.drawable.icon_delete);
			imgDelete.setId(1002);
			imgDelete.setOnClickListener(new WorkoutRemover(i));
			rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			rlp.addRule(RelativeLayout.LEFT_OF, imgStart.getId());
			rlp.addRule(RelativeLayout.CENTER_VERTICAL);
			
			rly.addView(imgDelete, rlp);
			
			// Edit Button
			imgEdit = new ImageView(this);
			imgEdit.setImageResource(R.drawable.icon_edit);
			imgEdit.setId(1003);
			imgEdit.setOnClickListener(new WorkoutHandler(true, i));
			rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			rlp.addRule(RelativeLayout.LEFT_OF, imgDelete.getId());
			rlp.addRule(RelativeLayout.CENTER_VERTICAL);
			
			rly.addView(imgEdit, rlp);
			
			// Workout Name
			tv = new TextView(this);
			tv.setText(w.getName());
			tv.setTextSize(20f);
			tv.setGravity(Gravity.CENTER);
			rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
			rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			rlp.addRule(RelativeLayout.LEFT_OF,imgEdit.getId());
			rlp.addRule(RelativeLayout.CENTER_VERTICAL);
			//Log.e("Trace","tv id: " + tv.getId());
			rly.addView(tv, rlp);
			
			if (w.isScheduled()) {
				_llyScheduled.addView(rly);
			} else {
				_llyUnscheduled.addView(rly);
			}
			
		} // end for i (iterate through workouts)
		
	} // end showWorkouts()
	
	/**
	 * Clears all buttons and elements in both scheduled and unscheduled listings
	 */
	private final void clearWorkoutButtons() {
		// This will only clear the buttons
		/*if (_llyScheduled.getChildCount() > 0) {
			_llyScheduled.removeAllViews();
		}
		if (_llyUnscheduled.getChildCount() > 0) {
			_llyUnscheduled.removeAllViews();
		}*/
		_llyScheduled.removeAllViews();
		_llyUnscheduled.removeAllViews();
	}
	
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	
	/**
	 * Utility class to handle how WorkoutAddEditActivity should be started.
	 * 
	 * @author Rob C
	 * @version 1.0
	 * @see OnClickListener
	 */
	private class WorkoutHandler implements OnClickListener {
		
		private boolean _edit;
		private int 	_slot;
		
		/**
		 * @param edit	Whether or not the next screen should edit an existing entry
		 * @param slot	Only necessary when edit is true; targets Workout by id
		 * 
		 * TODO Send SQLite id to WorkoutAddEditActivity
		 */
		public WorkoutHandler(boolean edit, int slot) {
			_edit	= edit;
			_slot 	= slot;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Use SQLite
			Intent i = new Intent(v.getContext(), WorkoutAddEditActivity.class);
			//Log.e("Workout","Slot: " + _slot);
			i.putExtra("slot", _slot);
			startActivity(i);
			
		} // end onClick()
		
	} // end inner class WorkoutHandler
	
	/**
	 * Utility class to delete a workout based on provided slot number.
	 * 
	 * @author Rob C
	 * @version 1.0
	 * @see OnClickListener
	 */
	private class WorkoutRemover implements OnClickListener {
		
		private int _wslot;
		
		/**
		 * @param wslot	Workout slot number to delete
		 * 
		 * TODO Use SQLite Workout.id to target a row for deletion
		 */
		public WorkoutRemover(int wslot) {
			_wslot = wslot;
		}
		
		@Override
		public void onClick(View v) {
			// TODO Popup dialog maybe
			MainActivity._workouts.remove(_wslot);
			clearWorkoutButtons();
			showWorkouts();
			
		} // end onClick()
		
	} // end inline class WorkoutRemover
	
	/**
	 * Utility class to start the Workout activity
	 * 
	 * @author Rob C
	 * @Version 1.0
	 */
	private class WorkoutStarter implements OnClickListener {
		
		private int _wslot;
		
		/**
		 * @param wslot	Workout slot number to start
		 * 
		 * TODO Use SQLite Workout.id to target a Workout to start
		 */
		public WorkoutStarter(int wslot) {
			_wslot = wslot;
		}
		
		@Override
		public void onClick(View v) {
			Intent i = new Intent(WorkoutActivity.this, ProgressActivity.class);
			i.putExtra("workoutslot", _wslot);
			startActivity(i);
		}
		
	} // end inline class WorkoutStarter
	
} // end class WorkoutActivity